﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Homework3
{
    public partial class MainWindow : Form
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void QueryButton_Click(object sender, EventArgs e)
        {
            ResultsTextBox.Clear();

            var lore = new Lore();
            lore.SetupOaSlot(0, "biblio");
            lore.SetupOaSlot(1, "doc", 0);
            lore.SetupOaSlot(2, "topic", 1);
            lore.SetupOaSlot(3, "auth-set", 1);
            lore.SetupOaSlot(4, "auth-ln", 3);

            /* SELECT T
             * FROM biblio.doc D, D.auth-set A, D.topic T
             * WHERE EXISTS L in A.auth-ln : L = "Ullman"
             */
            lore.PushOperation(Lore.OperatorType.Scan,   new List<object> { 4 });                // Join 1 Right 4
            lore.PushOperation(Lore.OperatorType.Select, new List<object> { 4, "=", "Ullman" }); // Join 1 Right 3

            // These two don't make sense in my current implementation and from my understanding
            // by reading the Lore report at http://infolab.stanford.edu/lore/pubs/lore97.pdf
            // As I understand, Select is nearly identical to its corresponding relational operator...
            // a relational Select only returns values which match its condition, which is how I have implemented
            // my Select operation. Thus, the Select existing further down the right branch of the Join already limits
            // what is coming back using these unnecessary Select and Aggr...?
            //lore.PushOperation(Lore.OperatorType.Aggr,      new List<object> { "exists", 4, 5 });   // Join 1 Right 2
            //lore.PushOperation(Lore.OperatorType.Select,    new List<object> { 5, "=", "true" });   // Join 1 Right 1

            lore.PushOperation(Lore.OperatorType.Scan, new List<object> { 3 }); // Join 2 Right
            lore.PushOperation(Lore.OperatorType.Scan, new List<object> { 2 }); // Join 3 Right
            lore.PushOperation(Lore.OperatorType.Scan, new List<object> { 1 }); // Join 4 Right
            lore.PushOperation(Lore.OperatorType.Scan, new List<object> { 0 }); // Join 4 Left
            lore.PushOperation(Lore.OperatorType.Join); // Join 4 (Join 3 Left)
            lore.PushOperation(Lore.OperatorType.Join); // Join 3 (Join 2 Left)
            lore.PushOperation(Lore.OperatorType.Join); // Join 2 (Join 1 Left)
            lore.PushOperation(Lore.OperatorType.Join); // Join 1 (From Project call)
            lore.PushOperation(Lore.OperatorType.Project, new List<object> { 2 });

            foreach (var obj in lore.NextOperator())
            {
                ResultsTextBox.Text += (obj + ", ");
            }
            ResultsTextBox.Text = ResultsTextBox.Text.Remove(ResultsTextBox.TextLength - 2, 2);
        }
    }
}
